"""
FIVEDASH(TM) -- General Purpose Accounting Software
Copyright (C) 2008  Obol Software Pty Ltd

This program is free software: you can redistribute it and/or modify
it. For terms and conditions, see fivedash/license/license.txt

"""

from phunc.template import Template as  Template
from phunc.logger import logger

##def form_text(
##        label = None,
##        id = None,
##        name = None,
##        classid = None,
##        style = None,
##        value = None,
##        disabled = False,
##        ):
##        pass
##def form_textarea(
##        label = None,
##        id = None,
##        name = None,
##        classid = None,
##        style = None,
##        value = None,
##        disabled = False,
##        ):
##        pass
##def form_select(
##        label = None,
##        id = None,
##        name = None,
##        classid = None,
##        style = None,
##        onchange = None,
##        option = [{}],
##        option_value = {'split':None,'items':['id'],'default_value':''},
##        option_text = {'split':None,'items':['name'],'default_text':'-- Please select --'},
##        selected_type = 1,#1:equal default_value 2:option value equal selected value 3:['id'](from the result set) equal selected value
##        selected_value = '',
##        selected_key = None,#if selected_type==3,set the key value
##        ):
##        pass
##def form_hidden(        
##        id = None,
##        name = None,
##        value = None,
##        ):
##        pass
##def form_button(
##        id = None,
##        name = None,
##        classid = None,
##        style = None,
##        value = None,
##        onclick = None,
##        ):pass
##def form_submit(
##        label = None,
##        id = None,
##        name = None,
##        value = None,
##        classid = None,
##        style = None,
##        ):
##        pass
##def form_reset(
##        label = None,
##        id = None,
##        name = None,
##        value = None,
##        ):
##        pass
##def form_checkbox(
##        checkbox_type = 1,#1:default create one by one; 2:loop auto create;
##        checkbox_value = [{}],
##        whole_label = None,
##		 prefix = False,
##		 prefix_text = None,
##        label = None,#{'type':1,'format':'give the label'}{'type':2,'format':'loop,a key in the value,maybe have a prefix'}
##        id = None,#{'type':1,'format':'give the id'}{'type':2,'format':'loop,a key in the value'}
##        name = None,#give the name
##        value = None,#{'type':1,'format':'give the id'}{'type':2,'format':'loop,a key in the value'}{'type':2,'format':'loop,a key in the value'}
##        format = 1,#1:horizontal;2:vertical;3:vertical,no whole_label;4:vertical,no whole_label,no label.just description(a key in the value);
##        ):
##        pass
##def form_radio(
##        radio_type = 1,#1:default create one by one; 2:loop auto create;
##        radio_value = [{}],
##        whole_label = None,
##        label = None,#{'type':1,'format':'give the label'}{'type':2,'format':'loop,a key in the value'}
##        id = None,#{'type':1,'format':'give the id'}{'type':2,'format':'loop,a key in the value'}
##        name = None,#give the name
##        value = None,#{'type':1,'format':'give the id'}{'type':2,'format':'loop,a key in the value'}
##        format = 1,#1:horizontal;2:vertical
##        ):
##        pass
##def form_file(
##        id = None,
##        name = None,
##        label = None,
##        value = None,
##        ):pass
##def form_div(
##        id = None,
##        name = None,
##        classid = None,
##        style = None,
##        ):
##        pass
##def textNode(
##        label = None,
##        flag = 1,#0:None;1:<span>value</span>;2:<div>value</div>;3:<p>value</p>
##        classid = None,
##        value = None,
##):
##        pass
##def line(
##        classid=None,
##        flag=0,#0:<hr />;1:other,may be <div class="lin1">&nbsp;</div>
##):
##        pass
def user_form(
		form_type = 1,#1:default;2:complex
		id = None,		
		classid = None,#class
		style = None,
		action = None,
		method = 'post',
		enctype = None,
		onsubmit = None,
		onreset = None,
		fieldsets = [],    
		form_bottom = {'type':'div','id':None,'classid':'bottom input','style':None,'items':[{'type':'submit','id':'submit','name':'submit','value':'Create'},{'type':'hidden','id':None,'name':None,'value':''}]},
		template_dir = '',
		):		
		for fieldset in fieldsets:#fieldset list fieldset
				for item in fieldset['items']:#element list item
						#select
						if item['type'] == 'select':
								option_list = []
								if item['option_value'].has_key('default_value'):
										option_map = {}
										option_map['value'] = item['option_value']['default_value']
										option_map['text'] = item['option_text']['default_text']
										option_list.append(option_map)
								if item['option']:                                        
										for option_item in item['option']:#create option
												option_map = {}
												option_value = None
												option_text = None
												if item['option_value']['split']:
														option_value = []
														for subitem in item['option_value']['items']:#option value item
																option_value.append(str(option_item[subitem]))
														option_value = item['option_value']['split'].join(option_value)
												else:
														option_value = option_item[item['option_value']['items'][0]]
												if item['option_text']['split']:
														option_text = []
														for subitem in item['option_text']['items']:#option value item
																option_text.append(option_item[subitem])
														option_text = item['option_text']['split'].join(option_text)
												else:
														option_text = option_item[item['option_text']['items'][0]]                                                
												option_map['text'] = option_text
												option_map['value'] = option_value
												if item['selected_type'] == 3:
														option_map['selected'] = option_item[selected_key]
												option_list.append(option_map)
								item['option_list'] = option_list
						#checkbox                                                
										
								
		content = {                
				'id':id,				
				'classid':classid,
				'style':style,
				'action':action,
				'method':method,
				'enctype':enctype,
				'onsubmit':onsubmit,
				'onreset':onreset,
				'fieldsets':fieldsets,
				'form_bottom':form_bottom,
		}
		template = Template()
		template.parseFile( template_dir + 'form.opt' )
		##        if form_type==1:
		##                template.parseFile( template_dir + 'form.opt' )
		##        elif form_type==2:
		##                template.parseFile( template_dir + 'form.opt' )
		html = template.render( content )
		return html

def user_div(
        id = None,
        name = None,
        classid = None,#class
        template_dir = '',
        list=None,
        style=None,
        isfo=None,
        title1=None,
        key=None,
        main=None,
        detail=None,
        item=None,
        devide=None,
        a_img=None,
        a_script =None,
        a_value=None,
        list_type=1,   #default type
        img_offdate=None,
        img_edit=None,
        skin=None,
        isi_div1=None,
        input=None,
        isi=None,
    ):
    


    content = {                
        'id':id,
        'name':name,
        'classid':classid,
        'style':style,
        'list':list,
        'isfo':isfo,
        'title1':title1,
        'key':key,
        'main':main,
        'detail':detail,
        'item':item,
        'devide':devide,
        'a_img':a_img,
        'a_script':a_script,
        'a_value':a_value,
        'img_offdate':img_offdate,
        'img_edit':img_edit,
        'skin':skin,
        'isi_div1':isi_div1,
        'input':input,
        'isi':isi,
    }

    template = Template()
    if list_type ==1:
        template.parseFile( template_dir + 'div.opt' )
    else:
        template.parseFile( template_dir + 'div2.opt' )
    html = template.render( content )
    return html
